package sp.whereru;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class DayPickerDialog extends DialogFragment {

	boolean[] daysPicked;
	
	/* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface DayPickerDialogListener {
        public void onDayPickerDialogPositiveClick(DialogFragment dialog);
        public void onDayPickerDialogNegativeClick(DialogFragment dialog);
        public void onDayPickerMultiChoiceItemClick(DialogFragment dialog, DialogInterface iDialog, int which, boolean isChecked);
    }
    
    // Use this instance of the interface to deliver action events
    static DayPickerDialogListener mListener;
    
    public DayPickerDialog(boolean[] daysPicked) {
    	super();
    	this.daysPicked = daysPicked;
    }
    
    /* Call this to instantiate a new DayPickerDialog.
     * @param activity  The activity hosting the dialog, which must implement the
     *                  NoticeDialogListener to receive event callbacks.
     * @returns A new instance of NoticeDialog.
     * @throws  ClassCastException if the host activity does not
     *          implement NoticeDialogListener
     */
    public static DayPickerDialog newInstance(Activity activity, boolean[] daysPicked) {
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events with it
            mListener = (DayPickerDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement DayPickerDialogListener");
        }
        DayPickerDialog frag = new DayPickerDialog(daysPicked);
        return frag;
    }

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// keep record of the days picked at the beginning of creating the dialog

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		// Set the dialog title
		builder.setTitle(R.string.text_days)
		// Specify the list array, the items to be selected by default (null for none),
		// and the listener through which to receive callbacks when items are selected
		.setMultiChoiceItems(R.array.days, daysPicked,
				new DialogInterface.OnMultiChoiceClickListener() {
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				// Send the item click event back to the host activity
				mListener.onDayPickerMultiChoiceItemClick(DayPickerDialog.this, dialog, which, isChecked);				
			}
		})
		// Set the action buttons
		.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// Send the positive button event back to the host activity
                mListener.onDayPickerDialogPositiveClick(DayPickerDialog.this);
			}
		})
		.setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// Send the negative button event back to the host activity
                mListener.onDayPickerDialogNegativeClick(DayPickerDialog.this);
			}
		});

		return builder.create();
	}

}
